//
//  HYLWelcomeViewController.swift
//  iOSwechat
//
//  Created by apple on 16/2/29.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SDWebImage

class HYLWelcomeViewController: UIViewController {

    var icomImageViewBotton : NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        
        setupUserIcon()
        
        // 获取用户头像信息
        HYLUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil{
                 print("获取用户头像失败\(error)")
                return
            }
            self.setupUserIcon()
        }
    }
    // 设置用户头像
    private func setupUserIcon() {
        if let avatar_large = HYLUserAccountViewModel.sharedInstance.userAccout?.avatar_large {
            let url = NSURL(string: avatar_large)
            self.icomView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    
    
    // MARK: - 准备UI 布局
    func prepareUI(){
        // 1.添加子控件
        view.addSubview(bgImageView)
        view.addSubview(icomView)
        view.addSubview(nameLabel)
        
        // 2.设置约束
//        bgImageView.translatesAutoresizingMaskIntoConstraints = false
//        icomView.translatesAutoresizingMaskIntoConstraints = false
//        nameLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 3. 背景
        // VFL: 可视化
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgView": bgImageView]))
//        
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgView":bgImageView]))
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        //  头像
        // CenterX和父控件CenterX重合
//        view.addConstraint(NSLayoutConstraint(item: icomView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//         icomImageViewBotton = (NSLayoutConstraint(item: icomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
//        view.addConstraint(icomImageViewBotton!)
//        // 头像宽高
//        view.addConstraint(NSLayoutConstraint(item: icomView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: icomView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        icomView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        // 名字
        // CenterX和父控件CenterX重合
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: icomView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        view.addConstraint(NSLayoutConstraint(item: nameLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: icomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            // make.centerX.equalTo(iconImageView.snp_centerX)
            
            // 当约束view的属性和参照view的属性相同的时候,参照的属性可以省略
            make.centerX.equalTo(icomView)
            make.top.equalTo(icomView.snp_bottom).offset(16)
        }
    }
    
    // MARK: - 欢迎显示动画，完毕跳转到首页
    private func moveAnimation(){
        
        // 改变头像底部的约束
        // icomImageViewBotton?.constant = -UIScreen .mainScreen().bounds.height - icomImageViewBotton!.constant
        
        // 要更新的约束
        icomView.snp_updateConstraints { (make) -> Void in
            // 要更新约束
        make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))

        }
        
        // 动画
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
                self.view.layoutIfNeeded()

            }) { (_) -> Void in
                
                // label渐变动画
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.nameLabel.alpha = 1
                    }, completion: { (_) -> Void in
                    // 跳转回首页
                    //(UIApplication.sharedApplication().delegate as? AppDelegate)?.switchViewController(true)
                    // 类方法调用，
                        AppDelegate.outswitchViewController(true)
                })     
        }
    }
    
   
    
    
    
    // MARK: - 懒加载bg
    private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    // MARK: - 懒加载头像
    private lazy var icomView:UIImageView = {
        let icomView = UIImageView(image: UIImage(named: "avatar_default_big"))
       
        icomView.layer.cornerRadius = 42.5
        icomView.layer.masksToBounds = true
        
        return icomView
    }()
    // MARK: - 懒加载名字
    private lazy var nameLabel:UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "欢迎归来"
        label.sizeToFit()
        label.alpha = 0
        
        return label
    }()
    
}
